package gui;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import utils.Operations;

public class SchemaDiagramViewer extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private JButton pauseButton;
	private JButton exitButton;
	private JToolBar toolbar;
	
	private JLabel[] images = new JLabel[5];
	
	private BufferedImage[] imgs = new BufferedImage[5];
	
	public SchemaDiagramViewer() {
		loadImages();
		initComponents();	
	}
	
	private void initComponents() {
		setBackground(Color.white);
		setForeground(Color.black);
		setEnabled(false);
		setLayout(null);

		add(getToolbar());
		
		for(int i=0; i<images.length; i++)
			add(getImage(i));
	}
	
	private JToolBar getToolbar() {
		if (toolbar == null) {
			toolbar = new JToolBar();
			toolbar.setBounds(0, 0, 800, 42);
			toolbar.add(getExitButton());
			toolbar.add(getPauseButton());
			toolbar.setRollover(true);
			toolbar.setFloatable(false);			
		}
		return toolbar;
	}
	
	private JButton getExitButton() {
		if (exitButton == null) {
			exitButton = new JButton();
			exitButton.setText("Exit MMDSS");
			exitButton.setIcon(new ImageIcon(getClass().getResource("/door_out.png")));
			exitButton.setFocusable(false);
			exitButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			exitButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
			exitButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					exitButtonMouseMouseClicked(event);
				}
			});
		}
		return exitButton;
	}
	
	private JButton getPauseButton() {
		if (pauseButton == null) {
			pauseButton = new JButton();
			pauseButton.setText("Save & Pause");
			pauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/disk.png")));
			pauseButton.setToolTipText("Saves your current work progress and pauses the execution.");
			pauseButton.setFocusable(false);
			pauseButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			pauseButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
			pauseButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					pauseButtonMouseMouseClicked(event);
				}
			});
		}
		return pauseButton;
	}
	
	private JLabel getImage(int index) {
		JLabel img = new JLabel();
		
		ImageIcon ico = new ImageIcon(imgs[index]);
		img.setIcon(ico);
		img.setBounds(index * 50, 50, ico.getIconWidth(), ico.getIconHeight());
		img.setFocusable(false);
		img.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		img.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
		
		return img;
	}
	
	private void exitButtonMouseMouseClicked(MouseEvent event) {
		Operations.systemEnd();
	}
	
	private void pauseButtonMouseMouseClicked(MouseEvent event) {
	}
	
	private void loadImages() {	
		try {
			imgs[0] = ImageIO.read(this.getClass().getResource("/actor.png"));
			imgs[1] = ImageIO.read(this.getClass().getResource("/customer.png"));
			imgs[2] = ImageIO.read(this.getClass().getResource("/film.png"));
			imgs[3] = ImageIO.read(this.getClass().getResource("/sales.png"));
			imgs[4] = ImageIO.read(this.getClass().getResource("/staff.png"));
		} catch(IOException iox) {
		}
	}
}
